package com.itheima.controller;

import com.itheima.health.dto.RoleDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class RoleController {

    @Reference
    private RoleService roleService;


    //   查询所有角色
    @GetMapping("role/findAll")
    //  @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        try {
            List<Role> list = roleService.findAll();
            return  new Result(true, MessageConstant.QUERY_ROLES_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_ROLES_FAIL);
        }
    }

    @PostMapping("role/findPage")
    //  @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult =  roleService.findPage(queryPageBean);
            return  new Result(true, MessageConstant.QUERY_ROLES_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_ROLES_FAIL);
        }
    }


    @PostMapping("role/add")
    public Result add(@RequestBody RoleDTO roleDTO){
        try {

            roleService.saveRole(roleDTO);
            return  new Result(true, MessageConstant.ADD_ROLE_SUCCESS);


        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"更新失败！");
        }
    }


    @GetMapping("role/findpermissionInfoByRoleId/{id}")
    public Result findpermissionInfoByRoleId(@PathVariable("id") int id){
        try {
            Map map  =  roleService.findpermissionInfoByRoleId(id);
            return  new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    @DeleteMapping("role/deleteRoleById/{id}")
    public Result  deleteRoleById(@PathVariable("id") int id){
        try {
            Role role = new Role();
            role.setIs_delete(1);
            role.setId(id);
            roleService.updateById(role);

            return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }
}
